package com.fyzk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lenovo on 2018/4/19.
 */

public class TimeUtil {

    /**
     * 时间格式化为年月日
     *
     * @param date
     * @param type 0: yyyy-MM-dd HH:mm
     *             1: yyyy-MM-dd
     *             2: yyyy-MM-dd HH:mm:ss
     *             3: MM月dd日
     *             4: HH:mm
     * @return
     */
    public static String getTime(Date date, int type) {
        SimpleDateFormat format;
        if (0 == type)
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        else if (1 == type)
            format = new SimpleDateFormat("yyyy-MM-dd");
        else if (2 == type)
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        else if (3 == type)
            format = new SimpleDateFormat("MM月dd日");
        else
            format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    /**
     * string类型转换为long类型
     */
    public static long stringToLong(String strTime) {
        Date date = stringToDate(strTime); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    /**
     * date要转换的date类型的时间
     */
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * string类型转换为date类型
     */
    public static Date stringToDate(String strTime) {
        SimpleDateFormat formatter;
        Date date = null;
        String replace = strTime.replace("T", " ")
                .replace("Z", "");
        try {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = formatter.parse(replace);
        } catch (ParseException e) {
            try {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                date = formatter.parse(replace);
            } catch (ParseException e1) {
                try {
                    formatter = new SimpleDateFormat("yyyy-MM-dd");
                    date = formatter.parse(replace);
                } catch (ParseException e2) {
                    try {
                        formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                        date = formatter.parse(replace);
                    } catch (ParseException e3) {
                        try {
                            formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
                            date = formatter.parse(replace);
                        } catch (ParseException e4) {
                            try {
                                formatter = new SimpleDateFormat("yyyy年MM月dd日");
                                date = formatter.parse(replace);
                            } catch (ParseException e5) {
                            }
                        }
                    }
                }
            }
        }
        if (null != date && strTime.endsWith("Z")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, 8);
            date = calendar.getTime();
        }
        return date;
    }
}
